
function GalerieImageController( $scope, FicheManager ) {
    $scope.myInterval = 10000;
    $scope.direction = 'left';
    $scope.currentIndex = 0;

    var nextIndex = -1;
    var previousIndex = -1;
    $scope.nextImage = null;
    $scope.previousImage = null;

    var sejourId = parseInt(document.querySelector("#sejourId").value);
    $scope.sejourId = sejourId;

    var retourne_images_galerie = function () {

        $scope.scFiche = new FicheManager();
        $scope.scFiche.get_images_galerie( sejourId ).then( function(e) {

            $scope.slides = e.data;

            set_next_and_previous_image ();
        });
    }

    $scope.setCurrentSlideIndex = function (index) {
        $scope.direction = (index > $scope.currentIndex) ? 'left' : 'right';
        $scope.currentIndex = index;

        set_next_and_previous_image ();
    };

    $scope.isCurrentSlideIndex = function (index) {
        return $scope.currentIndex === index;
    };

    $scope.prevSlide = function () {
        $scope.direction = 'left';
        $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
        
        set_next_and_previous_image ();
    };

    $scope.nextSlide = function () {
        $scope.direction = 'right';
        $scope.currentIndex = ($scope.currentIndex > 0) ? --$scope.currentIndex : $scope.slides.length - 1;
            
        set_next_and_previous_image ();
    };

    var set_next_and_previous_image = function () {
        
        var lg = parseInt($scope.slides.length) - 1;

        nextIndex = ( $scope.currentIndex == 0 ) ? lg : $scope.currentIndex - 1 ;
        
        previousIndex = ( $scope.currentIndex == lg ) ? 0 : $scope.currentIndex + 1 ;

        $scope.nextImage = $scope.slides[ nextIndex ].image;

        $scope.previousImage = $scope.slides[ previousIndex ].image;
    }

    /* charge les images de la galerie */
    retourne_images_galerie ();
}   
